<?php
$factoryServices = new FactoryServices();	
$bankAccountService = new BankAccountServices();
switch ($action) {
	case "add":
		if($permit == true)
		{
			$data = array();
			foreach ($_POST as $key => $value) {
				$data[$key] = $value;
			}
			$factoryDomain = new FactoryDomain($data);
			
			$bankAccountDomain1 = new BankaccountDomain(null);
			$bankAccountDomain1->setAccountNumber($data['accountNumber1']);
			$bankAccountDomain1->setBankID($data['factoryBank1']);
			$bankAccountID1 = $bankAccountService->create($context, $result, $bankAccountDomain1);
			$factoryDomain->setBankAccountID1($bankAccountID1);

			$bankAccountDomain2 = new BankaccountDomain(null);
			$bankAccountDomain2->setAccountNumber($data['accountNumber2']);
			$bankAccountDomain2->setBankID($data['factoryBank2']);
			$bankAccountID2 = $bankAccountService->create($context, $result, $bankAccountDomain2);
			$factoryDomain->setBankAccountID2($bankAccountID2);
			
			$userDomain = new UserDomain($data);
			$factory = new Factory($factoryDomain);
			$factory->setUserDomain($userDomain);
			$factoryServices->createFactory($context, $result, $factory);
			//if($result->isFailed())
				//echo "<script>alert('".$result->getMessage()."');window.location='index.php?func=fact&opt=list';</script></script>";
			header("Location:index.php?func=fact&opt=list");
		}
		else
			header("Location:index.php?func=dash");
	break;
	
	case "delete":
		if($permit == true)
		{
			if(isset($_GET['id']))
			{
				$factory = $factoryServices->getFactoryByOid($context, $result, $_GET['id']);
				$factoryServices->deleteFactoryByOid($context, $result, $factory);
				header("Location:index.php?func=fact&opt=list");
			}
		}
		else
			header("Location:index.php?func=dash");
	break;
	
	case "edit":
		if($permit == true)
		{
			$data = array();
			foreach ($_POST as $key => $value) {
				$data[$key] = $value;
			}
			$factoryDomain = new FactoryDomain($data);
			$factory = $factoryServices->getFactoryByOid($context, $result, $data['id']);
			
			$bankAccountDomain1 = new BankaccountDomain(null);
			$bankAccountDomain1->setAccountNumber($data['accountNumber1']);
			$bankAccountDomain1->setBankID($data['factoryBank1']);
			
			if($factory->getFactoryDomain()->getBankAccountID1() != "null")
			{
				$bankAccountDomain1->setBankAccountID($factory->getFactoryDomain()->getBankAccountID1());				
				$bankAccountService->update($context, $result, $bankAccountDomain1);
				$factoryDomain->setBankAccountID1($factory->getFactoryDomain()->getBankAccountID1());
			}
			else 
			{
				$bankAccountID1 = $bankAccountService->create($context, $result, $bankAccountDomain1);
				$factoryDomain->setBankAccountID1($bankAccountID1);				
			}
			
			$bankAccountDomain2 = new BankaccountDomain(null);
			$bankAccountDomain2->setAccountNumber($data['accountNumber2']);
			$bankAccountDomain2->setBankID($data['factoryBank2']);
			if($factory->getFactoryDomain()->getBankAccountID2() != "null")
			{
				$bankAccountDomain2->setBankAccountID($factory->getFactoryDomain()->getBankAccountID2());				
				$bankAccountService->update($context, $result, $bankAccountDomain2);
				$factoryDomain->setBankAccountID2($factory->getFactoryDomain()->getBankAccountID2());
			}
			else 
			{
				$bankAccountID2 = $bankAccountService->create($context, $result, $bankAccountDomain2);
				$factoryDomain->setBankAccountID2($bankAccountID2);				
			}
			$factory->setFactoryDomain($factoryDomain);
			$factory->getFactoryDomain()->setCountryID($data['countryID']);			
			$factoryServices->updateFactory($context, $result, $factory);
			header("Location:index.php?func=fact&opt=list");
		}
		else
			header("Location:index.php?func=dash");
	break;
	
	default:
		echo "khong co tinh nang nay";
	break;
}
?>	